﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;

using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMCopyBOMToLabourCost : BaseQryRpt 
    {
        public enum FromType
        {
            Category,    //类别工序BOM
            BOM,         //标准BOM
            BOMRev,      //产成品核价 
            BOMSales,       //产成品报价 
        }

        public struct Parameter
        {
            public string strCategoryID;
            public string strSetID;
            public string strCategoryName;
            public string strSetName;
            public string strProductFitemID ;
            public string strProductName;
            public string strTechnicsName;
            public string strVerNo;
        }
        private Parameter mPara = new Parameter();
        private string mstrCategoryLabourCostTableName = "rmCategorySetBOMLabourCost";
        private string mstrBOMLabourCostTableName = "rmProductBOMLabourCost";
        private string mstrBOMRevLabourCostTableName = "rmProductBOMLabourCostRev";
        private string mstrBOMSalesLabourCostTableName = "rmProductBOMLabourCostSales";
        public bool mblnHasReturn = false ;
        private FromType mFromType = new FromType();
        private DBAccess mdbcon = new DBAccess(false, false);
        public frmRMCopyBOMToLabourCost()
        {
            InitializeComponent();           
            LoadAllControl(new ArrayList(this.Controls));          
            this.MinimumSize = this.Size;
        }

        public void ShowFrom(FromType fromType, DataGridView dgvView, Parameter para)
        {
            this.Text = "复制  BOM工序   到   项目分摊";
            mFromType = fromType;
            mPara = para;
            InitGrid(dgvView, dgvBOM);
            this.Width = 1024;
            this.ShowDialog(); 
        }

        private void SetExistsRowReadOnly()
        {
            string strWKID = "";
            string strFItemID = "";

            string strSQL = "";

            if (mFromType == FromType.BOM)
                strSQL = "select * from " + mstrBOMLabourCostTableName + " where  ProductFitemID='" + mPara.strProductFitemID  + "' and TechnicsName='" + mPara.strTechnicsName + "'";
            else if (mFromType == FromType.BOMRev)
                strSQL = "select * from " + mstrBOMRevLabourCostTableName + " where ProductFitemID='" + mPara.strProductFitemID + "' and VersionNo='" + mPara.strVerNo + "' ";
            else if (mFromType == FromType.Category)
                strSQL = "select * from "+ mstrCategoryLabourCostTableName +" where CategoryID='"+ mPara.strCategoryID  +"' and SetID='"+ mPara.strSetID +"' ";
            else if (mFromType == FromType.BOMSales)
                strSQL = "select * from " + mstrBOMSalesLabourCostTableName + " where ProductFitemID='" + mPara.strProductFitemID + "' and VersionNo='" + mPara.strVerNo + "' ";
            DataTable dtExists = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtExists.Rows)
            {
                strWKID = CvtFunc.ToString(drRow["WKID"]);
                strFItemID = CvtFunc.ToString(drRow["FItemID"]);
                foreach (DataGridViewRow dgvRow in this.dgvBOM.Rows)
                {
                    if (CvtFunc.ToString(dgvRow.Cells["WKID"].Value) == strWKID && CvtFunc.ToString(dgvRow.Cells["FItemID"].Value) == strFItemID)
                    {
                        dgvRow.ReadOnly = true;
                        for (int i = 0; i <= this.dgvBOM.Columns.Count - 1; i++)
                        {
                            dgvRow.Cells[i].Style.BackColor = Color.AliceBlue;
                        }
                    }
                }
            }
        }

        private void InitGrid(DataGridView dgvView, DataGridView dgvBOM)
        {
            dgvBOM.Columns.Clear();
            foreach (DataGridViewColumn dgvCol in dgvView.Columns)
            {
                if (dgvCol.Name != "DelLine")
                {
                    DataGridViewTextBoxColumn dgvNewCol = new DataGridViewTextBoxColumn();

                    dgvNewCol.HeaderText = dgvCol.HeaderText;
                    dgvNewCol.Width = dgvCol.Width;
                    dgvNewCol.Visible = dgvCol.Visible;
                    dgvNewCol.ReadOnly = dgvCol.ReadOnly;
                    dgvNewCol.Name = dgvCol.Name;
                    dgvNewCol.DefaultHeaderCellType = dgvCol.DefaultHeaderCellType;
                    dgvNewCol.DefaultCellStyle = dgvCol.DefaultCellStyle;
                    dgvBOM.Columns.Add(dgvNewCol);
                }
            }
            
            foreach (DataGridViewRow dgvRow in dgvView.Rows)
            {
                dgvBOM.Rows.Add();
                for ( int i=0; i<= dgvView.Columns.Count -2;i++)
                {
                   dgvBOM[i, dgvRow.Index].Value  = dgvRow.Cells[i].Value ;
                }                
            }
            DataGridViewCheckBoxColumn  colSel = new DataGridViewCheckBoxColumn();
            colSel.HeaderText  = "选择";
            colSel.Name = "SEL";
            colSel.Width = 60;
            this.dgvBOM.Columns.Add(colSel);
            SetExistsRowReadOnly();
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.dgvBOM.EndEdit();
            mblnHasReturn = true;
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CmdSel_Click(object sender, EventArgs e)
        {

        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.dgvBOM.Rows)
            {
               if ( dgvRow.ReadOnly != true) dgvRow.Cells["SEL"].Value =  true ;
            }
            this.dgvBOM.EndEdit();
        }

        private void btnUnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.dgvBOM.Rows)
            {
                dgvRow.Cells["SEL"].Value = false ;
            }
            this.dgvBOM.EndEdit();
        }


    }
}
